import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/parallelBranch'

const api = {

    /**
     * 新增并行分支
     * @param parallelBranch
     * @returns {Promise<*|undefined>}
     */
    createParallelBranch: (parallelBranch) =>
      axiosClient().post(`${requestUrl}/addInfo`, parallelBranch),

    /**
     * 根据ID删除并行分支
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteParallelBranchById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新并行分支
     * @param id
     * @param parallelBranch
     * @returns {Promise<*|undefined>}
     */
    updateParallelBranchFull: (id, parallelBranch) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, parallelBranch),

    /**
     * 根据ID部分更新并行分支
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateParallelBranchPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询并行分支
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getParallelBranchById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询并行分支
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getParallelBranchPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询并行分支
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getParallelBranchList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.gatewayId) {
            params.append('gatewayId', businessParam.gatewayId)
        }
        if (businessParam.branchName) {
            params.append('branchName', businessParam.branchName)
        }
        if (businessParam.targetNodeId) {
            params.append('targetNodeId', businessParam.targetNodeId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
